package com.moonbeom.pptimer;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class MainActivity extends Activity {
	final int FINISH = -1;

	EditText numSlideEdit;
	EditText alarmTimeEdit;
	EditText remindTimeEdit;

	Button startBtn;
	Button stopBtn;

	TextView infoText;

	Vibrator tVibrator;

	TimeThread tThread;
	Handler tHandler;

	Toast toast;

	long[] alarmPattern = {500, 500};
	long[] finishPattern = {1000, 2000};

	int alarmTime;
	int remindTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		numSlideEdit = (EditText) findViewById(R.id.slidesEdit);
		alarmTimeEdit = (EditText) findViewById(R.id.alarmTimeEdit);
		remindTimeEdit = (EditText) findViewById(R.id.remindTimeEdit);

		startBtn = (Button) findViewById(R.id.alarmStart);
		stopBtn = (Button) findViewById(R.id.alarmStop);

		infoText = (TextView) findViewById(R.id.infoText);


		numSlideEdit.setText("0");
		alarmTimeEdit.setText("0");
		remindTimeEdit.setText("0");

		tVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		tThread = null;
		tHandler = null;

		startBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				alarmTime = Integer.parseInt(alarmTimeEdit.getText().toString());
				remindTime = Integer.parseInt(remindTimeEdit.getText().toString());

				if(remindTime >= alarmTime){
					toast = Toast.makeText(v.getContext(), "remindTime should be smaller than alarmTime", Toast.LENGTH_LONG);
					toast.show();
				}

				else{

					if(tThread == null){
						tThread = new TimeThread();
						tThread.setInfoText(infoText);

						if(tHandler == null){
							tHandler = new Handler(){

								@Override
								public void handleMessage(Message msg) {
									// TODO Auto-generated method stub
									synchronized (tHandler) {
										if(msg.arg1 == FINISH){
											tVibrator.vibrate(finishPattern, -1);
											viewText("0 slide : 0 second");
										}

										else{

											if(msg.arg2 == remindTime && remindTime > 0)
												tVibrator.vibrate(200);
											if(msg.arg2 == 0)
												tVibrator.vibrate(alarmPattern, -1);

											viewText(msg.arg1 + " slide : " + msg.arg2 + " second");
										}
									}
								}

							};
						}

						tThread.setHandler(tHandler);
						tThread.setTime(Integer.parseInt(numSlideEdit.getText().toString()), alarmTime);
						tThread.start();
					}

					else if(tThread.isWorking == false){
						tThread.setTime(Integer.parseInt(numSlideEdit.getText().toString()), alarmTime);
						tThread.setWorking(true);
					}
				}
			}
		});

		stopBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(tThread != null){
					tThread.setWorking(false);
					viewText("0 slide : 0 second");
					toast = Toast.makeText(v.getContext(), "PPTimer is stoped", Toast.LENGTH_LONG);
					toast.show();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub

		if(tHandler != null)
			tHandler.getLooper().quit();

		tThread.setRun(false);

		super.onDestroy();
	}

	public void viewText(String text){
		infoText.setText(text);
	}
}
